<?php

namespace App\Transformers\User;

use App\Models\User;
use App\Transformers\Order\OrderTransformer;
use App\Transformers\StoreCoupon\StoreCouponUserTransformer;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    //可进行包含的信息
    protected $availableIncludes = [
        'userlabel',
        'orders',
        'usersigns',
        'storecouponuser',
        'userfriends',
        'userbills'
    ];

    public function transform(User $user)
    {
        return [
            'id' => $user->id,
            'avatar' => $user->avatar ?? '',
            'avatar_url' => 'https://placeimg.com/640/480/any',
            'account' => $user->account ?? '',
            'real_name' => $user->real_name ?? '',
            'sex' => $user->sex ?? '',
            'birthday' => $user->birthday ?? '',
            'card_id' => $user->card_id ?? '',
            'nickname' => $user->nickname ?? '',
            'email' => $user->email ?? '',
            'phone' => $user->phone ?? '',
            'level' => $user->level ?? '',
            'balance' => $user->balance ?? '',
            'addres' => $user->addres ?? '',
            'integral' => $user->integral ?? '',
            'status' => $user->status ?? 1,
            'group_id' => $user->group_id ?? '',
            'remark' => $user->remark ?? '',
            'partner_id' => $user->partner_id ?? '',
            'sign_num' => $user->sign_num ?? '',
            'last_ip' => $user->last_ip ?? '',
            'last_time' => $user->last_time ?? '',
            'pay_count' => $user->pay_count ?? '',
            'country' => $user->country ?? '',
            'brokerage_price' => $user->brokerage_price ?? '',
            'experience' => $user->experience ?? '',
            'user_type' => $user->user_type ?? '',
            'user_typec' => $user->userTypeEnum[$user->user_type] ?? '',
            'login_type' => $user->login_type ?? '',
            'login_typec' => $user->loginTypeEnum[$user->login_type] ?? '',
            'is_promoter' => $user->is_promoter ?? 0,
            'is_extension' => $user->is_extension ?? 0,
            'promoter_id' => $user->promoter_id ?? '',
            'promoter_time' => $user->promoter_time ?? '',
            'spread_count' => $user->spread_count ?? '',
            'system_admin_id' => $user->system_admin_id ?? '',
            'record_phone' => $user->record_phone ?? '',
            'is_member' => $user->is_member ?? 0,
            'member_time' => $user->member_time ?? '',
            'member_source' => $user->member_source ?? '',
            'member_source_type' => $user->memberSourceEnum[$user->member_source] ?? '',
            'is_forever_member' => $user->is_forever_member ?? 0,
            'uniqid' => $user->uniqid ?? '',
            'created_at' => $user->created_at ? $user->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $user->updated_at ? $user->updated_at->format("Y-m-d H:i:s") : '',
            'catrgory' => $user->catrgory ?? '',
        ];
    }

    //包含用户相关的标签的信息
    public function includeUserlabel(User $user)
    {
        //返回用户相关的表签的信息   使用用户模型中的关联的用户标签的方法   使用用户标签表的 transformer进行相应的格式返回
        return $this->collection($user->userlabel, new UserLabelsTransformer());
        // item() 为获取单条的信息  collection() 为返回一个集合 多条信息
    }

    //包含用户乡相关的好友的信息
    public function includeUserFriends(User $user)
    {
        return $this->collection($user->userfriends, new UserTransformer());
        // item() 为获取单条的信息  collection() 为返回一个集合 多条信息
    }

    //获取用户订单的信息
    public function includeOrders(User $user)
    {
        return $this->collection($user->orders, new OrderTransformer());
    }

    //获取到用户的签到记录
    public function includeUserSigns(User $user)
    {
        return $this->collection($user->usersigns, new UserSignTransFormer());
    }

    //获取用户所拥有的优惠券
    public function includestorecouponuser(User $user)
    {
        return $this->collection($user->storecouponuser, new StoreCouponUserTransformer());
    }

    //用户对应的账单的信息
    public function includeuserbills(User $user)
    {
        return $this->collection($user->userbill, new UserBillsTransformer());
    }

    //包含用户相关的订单信息
    public function includeOrder(User $user)
    {
        return $this->collection($user->order, new OrderTransformer());
    }

    //用户所包含的提现记录
    public function includeUserExtract(User $user)
    {
        return $this->collection($user->userextract, new UserExtractsTransformer());
    }
}
